package com.boot.study.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.boot.study.domain.DbDriftDatabase;
import com.boot.study.mapper.DbDriftDatabaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Author likz
 * @Date 2024/8/7 9:22
 **/
@Component
@Slf4j
public class DynamicDatasourceConfig {


    @Resource
    private DbDriftDatabaseMapper edcDbDriftDatabaseMapper;


    @Resource
    private DynamicRoutingDataSource dynamicRoutingDataSource;


    @PostConstruct
    public void loadAllDB(){
        List<DbDriftDatabase> edcDbDriftDatabases = edcDbDriftDatabaseMapper.selectList(null);
        for (DbDriftDatabase edcDbDriftDatabase : edcDbDriftDatabases) {
            DruidDataSource dataSource = JdbcUtils.getInitDBConfig();
            dataSource.setUsername(edcDbDriftDatabase.getDbusername());
            dataSource.setPassword(edcDbDriftDatabase.getDbpassword());
            dataSource.setUrl(edcDbDriftDatabase.getUrl()+"/"+edcDbDriftDatabase.getDbname()+"?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true");
            dynamicRoutingDataSource.addDataSource(edcDbDriftDatabase.getProjectId(),dataSource);
            log.info("======加载动态数据库完成：ProjectId="+edcDbDriftDatabase.getProjectId());
        }
    }

    public void addDB(String projectId){
       // DbDriftDatabase dbDriftDatabase = edcDbDriftDatabaseMapper.selectOne(projectId);
        DruidDataSource dataSource = JdbcUtils.getInitDBConfig();
//        dataSource.setUsername(dbDriftDatabase.getDbusername());
//        dataSource.setPassword(dbDriftDatabase.getDbpassword());
//        dataSource.setUrl(dbDriftDatabase.getUrl()+"/"+dbDriftDatabase.getDbname()+"?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true");
//        dynamicRoutingDataSource.addDataSource(dbDriftDatabase.getProjectId(),dataSource);
//        log.info("======加载动态数据库完成：ProjectId="+dbDriftDatabase.getProjectId());
//        Object object = redisService.get("DB:" + projectId);
//       // Map<String,String> map = BeanUtil.toBean(object, Map.class);
//        dataSource.setUsername(map.get("username"));
//        dataSource.setPassword(map.get("password"));
//        dataSource.setUrl(map.get("url")+"/"+map.get("dbname")+"?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true");
//        dynamicRoutingDataSource.addDataSource(map.get("projectId"),dataSource);
//        log.info("======加载动态数据库完成：ProjectId="+map.get("projectId"));
        //redisService.del("DB:" + projectId);
    }

    /**
     * 删除DB 展示不需要调用
     */
    public void deleteDB(String projectId){
        dynamicRoutingDataSource.removeDataSource(projectId);
    }


}
